package com.yuwenxin.dao.impl;

import com.yuwenxin.bean.Notice;
import com.yuwenxin.bean.User;
import com.yuwenxin.core.BaseDaoImpl;
import com.yuwenxin.core.BaseQueryModel;
import com.yuwenxin.dao.NoticeDao;
import com.yuwenxin.dao.UserDao;

import java.sql.ResultSet;
import java.sql.Date;

public class NoticeDaoImpl extends BaseDaoImpl implements NoticeDao {
    public Notice getLatestNotice(){
        return null;
    }

    @Override
    public String getSelectAllSql() {
        return "select * from notice";
    }

    @Override
    public String getSelectByNameSql() {
        return "select * from notice where noticeTitle = ?";
    }

    @Override
    public String getFuzzyFindSql() {
        return "select * from notice where noticeTitle like ?";
    }

    @Override
    public String getUpdateSql(Object object) {
        return null;
    }

    @Override
    public String getDeleteSql() {
        return "delete from notice where idnotice = ?";
    }

    @Override
    public String getSelectByIdSql() {
        return "select * from notice where idnotice = ?";
    }

    @Override
    public String getInsertSql(Object object) {
        Notice _notice = (Notice) object;
        String noticeTitle = _notice.getNoticeTitle();
        String context = _notice.getContext();
        Date postDate = new Date(new java.util.Date().getTime());  // 直接获取系统当前时间插入作为notice发布时间
        int noticeUserid = _notice.getNoticeUserId();
        return "insert into notice (noticeTitle,context,postDate,noticeUserid) values ("
                + noticeTitle + "','" + context + "','" + postDate + "','" + noticeUserid + ")";
    }

    @Override
    public String getConditionSql(BaseQueryModel queryModel) {
        return null;
    }

    @Override
    public Object toEntity(ResultSet rs) throws Exception {
        Notice notice = new Notice();

        UserDao userDao = new UserDaoImpl();

        notice.setId(rs.getInt("idnotice"));
        notice.setNoticeTitle(rs.getString("noticeTitle"));
        notice.setContext(rs.getString("context"));
        notice.setPostDate(rs.getDate("postDate"));
        notice.setNoticeUserId(rs.getInt("noticeUserid"));
        notice.setPoster((User) userDao.find(rs.getInt("noticeUserid")));
        return notice;
    }
}
